using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.EventSystems;
using UnityEngine.SceneManagement;

public class InteractWithMap : MonoBehaviour,IPointerEnterHandler,IBeginDragHandler,IDragHandler,IEndDragHandler
{
    // Start is called before the first frame update
    public Camera cam;
    private bool canInteract = false;
    private Vector2 mousePosition;
    private Vector2 offset;
    private bool canDrag;
    private void Update()
    {
        Interact();
    }

    private void Awake()
    {
        
    }

    public void Interact()
    {
        if (SceneManager.GetActiveScene().name == "Village")
        {
            if (canInteract)
            {
                if (Input.GetAxis("Mouse ScrollWheel") > 0 && cam.orthographicSize > 0)
                {
                    cam.orthographicSize -= 1;
                }
                else if (Input.GetAxis("Mouse ScrollWheel") < 0 && cam.orthographicSize < 90)
                {
                    cam.orthographicSize += 1;
                }

                if (canDrag && cam.orthographicSize < 70)
                {
                    cam.transform.position -= new Vector3(offset.x, 0, offset.y) / 8000 * cam.orthographicSize;
                }
            }
        }
        else
        {
            if (canInteract)
            {
                if (Input.GetAxis("Mouse ScrollWheel") > 0 && cam.orthographicSize > 0)
                {
                    cam.orthographicSize -= 1;
                }
                else if (Input.GetAxis("Mouse ScrollWheel") < 0 && cam.orthographicSize < 180)
                {
                    cam.orthographicSize += 1;
                }

                if (canDrag && cam.orthographicSize < 150)
                {
                    cam.transform.position -= new Vector3(offset.x, 0, offset.y) / 20000 * cam.orthographicSize;
                }
            }
        }
    }

    public void OnPointerEnter(PointerEventData eventData)
    {
        if (eventData.pointerEnter == this.gameObject)
        {
            canInteract = true;
        }
    }

    public void OnBeginDrag(PointerEventData eventData)
    {
        mousePosition = eventData.position;
        canDrag = true;
    }

    public void OnDrag(PointerEventData eventData)
    {
        offset = eventData.position - mousePosition;
    }

    public void OnEndDrag(PointerEventData eventData)
    {
        canDrag = false;
    }
}
